#include "yspushbutton.h"
#include "mainwindow.h"
YSPushButton::YSPushButton(QWidget *parent)
    : TouchButton{parent}
{
    // 连接按钮按下和释放信号
    connect(this, &QPushButton::pressed, this, &YSPushButton::onPressed);
    connect(this, &QPushButton::released, this, &YSPushButton::onReleased);
}

void YSPushButton::setNormalIcon(const QIcon &icon)
{
    m_normalIcon=icon;
}

void YSPushButton::setPressedIcon(const QIcon &icon)
{
    m_pressedIcon=icon;
}

void YSPushButton::onPressed()
{
    setIcon(m_pressedIcon);
}

void YSPushButton::onReleased()
{
    setIcon(m_normalIcon);
}

void YSPushButton::showEvent(QShowEvent *event)
{
    Q_UNUSED(event)
    if(m_bFirst)
    {
        m_bFirst=false;
        // 计算新的按钮大小
        int newWidth = static_cast<int>(width() * g_xScale);
        int newHeight = static_cast<int>(height() * g_xScale);
        // 设置按钮的大小
        setFixedSize(newWidth, newHeight);
        QPixmap pixmap = m_pressedIcon.pixmap(m_pressedIcon.actualSize(QSize(40* g_xScale, 40* g_xScale)));
        m_pressedIcon = QIcon(pixmap);
         pixmap = m_normalIcon.pixmap(m_normalIcon.actualSize(QSize(40* g_xScale, 40* g_xScale)));
        m_normalIcon = QIcon(pixmap);
        setIcon(m_normalIcon);
        // 设置按钮的字号
        QFont font = this->font();
        font.setPixelSize(20*g_xScale);
        font.setFamily("微软雅黑");
        setFont(font);
    }
}
